/**
 *
 *
 *
 *
 *
 *
 */
package com.food.mall.service.impl;

import com.food.mall.common.Constants;
import com.food.mall.common.ServiceResultEnum;
import com.food.mall.controller.vo.FoodMallShoppingCartItemVO;
import com.food.mall.dao.FoodMallGoodsMapper;
import com.food.mall.dao.FoodMallShoppingCartItemMapper;
import com.food.mall.entity.FoodMallGoods;
import com.food.mall.entity.FoodMallShoppingCartItem;
import com.food.mall.service.FoodMallShoppingCartService;
import com.food.mall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class FoodMallShoppingCartServiceImpl implements FoodMallShoppingCartService {

    @Autowired
    private FoodMallShoppingCartItemMapper foodMallShoppingCartItemMapper;

    @Autowired
    private FoodMallGoodsMapper foodMallGoodsMapper;

    @Override
    public String saveFoodMallCartItem(FoodMallShoppingCartItem foodMallShoppingCartItem) {
        FoodMallShoppingCartItem temp = foodMallShoppingCartItemMapper.selectByUserIdAndGoodsId(foodMallShoppingCartItem.getUserId(), foodMallShoppingCartItem.getGoodsId());
        if (temp != null) {
            //已存在则修改该记录
            temp.setGoodsCount(foodMallShoppingCartItem.getGoodsCount());
            return updateFoodMallCartItem(temp);
        }
        FoodMallGoods foodMallGoods = foodMallGoodsMapper.selectByPrimaryKey(foodMallShoppingCartItem.getGoodsId());
        //商品为空
        if (foodMallGoods == null) {
            return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
        }
        int totalItem = foodMallShoppingCartItemMapper.selectCountByUserId(foodMallShoppingCartItem.getUserId()) + 1;
        //超出单个商品的最大数量
        if (foodMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //超出最大数量
        if (totalItem > Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_TOTAL_NUMBER_ERROR.getResult();
        }
        //保存记录
        if (foodMallShoppingCartItemMapper.insertSelective(foodMallShoppingCartItem) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateFoodMallCartItem(FoodMallShoppingCartItem foodMallShoppingCartItem) {
        FoodMallShoppingCartItem foodMallShoppingCartItemUpdate = foodMallShoppingCartItemMapper.selectByPrimaryKey(foodMallShoppingCartItem.getCartItemId());
        if (foodMallShoppingCartItemUpdate == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        //超出单个商品的最大数量
        if (foodMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //当前登录账号的userId与待修改的cartItem中userId不同，返回错误
        if (!foodMallShoppingCartItemUpdate.getUserId().equals(foodMallShoppingCartItem.getUserId())) {
            return ServiceResultEnum.NO_PERMISSION_ERROR.getResult();
        }
        //数值相同，则不执行数据操作
        if (foodMallShoppingCartItem.getGoodsCount().equals(foodMallShoppingCartItemUpdate.getGoodsCount())) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        foodMallShoppingCartItemUpdate.setGoodsCount(foodMallShoppingCartItem.getGoodsCount());
        foodMallShoppingCartItemUpdate.setUpdateTime(new Date());
        //修改记录
        if (foodMallShoppingCartItemMapper.updateByPrimaryKeySelective(foodMallShoppingCartItemUpdate) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public FoodMallShoppingCartItem getFoodMallCartItemById(Long foodMallShoppingCartItemId) {
        return foodMallShoppingCartItemMapper.selectByPrimaryKey(foodMallShoppingCartItemId);
    }

    @Override
    public Boolean deleteById(Long shoppingCartItemId, Long userId) {
        FoodMallShoppingCartItem foodMallShoppingCartItem = foodMallShoppingCartItemMapper.selectByPrimaryKey(shoppingCartItemId);
        if (foodMallShoppingCartItem == null) {
            return false;
        }
        //userId不同不能删除
        if (!userId.equals(foodMallShoppingCartItem.getUserId())) {
            return false;
        }
        return foodMallShoppingCartItemMapper.deleteByPrimaryKey(shoppingCartItemId) > 0;
    }

    @Override
    public List<FoodMallShoppingCartItemVO> getMyShoppingCartItems(Long foodMallUserId) {
        List<FoodMallShoppingCartItemVO> foodMallShoppingCartItemVOS = new ArrayList<>();
        List<FoodMallShoppingCartItem> foodMallShoppingCartItems = foodMallShoppingCartItemMapper.selectByUserId(foodMallUserId, Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER);
        if (!CollectionUtils.isEmpty(foodMallShoppingCartItems)) {
            //查询商品信息并做数据转换
            List<Long> foodMallGoodsIds = foodMallShoppingCartItems.stream().map(FoodMallShoppingCartItem::getGoodsId).collect(Collectors.toList());
            List<FoodMallGoods> foodMallGoods = foodMallGoodsMapper.selectByPrimaryKeys(foodMallGoodsIds);
            Map<Long, FoodMallGoods> foodMallGoodsMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(foodMallGoods)) {
                foodMallGoodsMap = foodMallGoods.stream().collect(Collectors.toMap(FoodMallGoods::getGoodsId, Function.identity(), (entity1, entity2) -> entity1));
            }
            for (FoodMallShoppingCartItem foodMallShoppingCartItem : foodMallShoppingCartItems) {
                FoodMallShoppingCartItemVO foodMallShoppingCartItemVO = new FoodMallShoppingCartItemVO();
                BeanUtil.copyProperties(foodMallShoppingCartItem, foodMallShoppingCartItemVO);
                if (foodMallGoodsMap.containsKey(foodMallShoppingCartItem.getGoodsId())) {
                    FoodMallGoods foodMallGoodsTemp = foodMallGoodsMap.get(foodMallShoppingCartItem.getGoodsId());
                    foodMallShoppingCartItemVO.setGoodsCoverImg(foodMallGoodsTemp.getGoodsCoverImg());
                    String goodsName = foodMallGoodsTemp.getGoodsName();
                    // 字符串过长导致文字超出的问题
                    if (goodsName.length() > 28) {
                        goodsName = goodsName.substring(0, 28) + "...";
                    }
                    foodMallShoppingCartItemVO.setGoodsName(goodsName);
                    foodMallShoppingCartItemVO.setSellingPrice(foodMallGoodsTemp.getSellingPrice());
                    foodMallShoppingCartItemVOS.add(foodMallShoppingCartItemVO);
                }
            }
        }
        return foodMallShoppingCartItemVOS;
    }
}
